package com.example.goodog.adapters;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;

import com.example.goodog.ComparatorByDistance;
import com.example.goodog.MyApplication;
import com.example.goodog.R;
import com.example.goodog.customViews.CallButton;
import com.example.goodog.customViews.DriveToButton;
import com.example.goodog.customViews.MailButton;
import com.example.goodog.customViews.ShowOnMapButton;
import com.example.goodog.customViews.ViewCalendarButton;
import com.example.goodog.customViews.WebSiteButton;
import com.example.goodog.interfaces.IShowCalendar;
import com.example.goodog.interfaces.IShowDriveToButton;
import com.example.goodog.interfaces.IShowMailButton;
import com.example.goodog.interfaces.IShowOnMap;
import com.example.goodog.interfaces.IShowPhoneButton;
import com.example.goodog.interfaces.IShowWebSiteButton;
import com.example.goodog.interfaces.MyComparator;
import com.example.goodog.items.ListItem;



public class ItemAdapter extends ArrayAdapter<ListItem> {
	protected Context context;
	protected ArrayList<ListItem> items;
	protected ArrayList<MyComparator> Comparators;
	protected int CurrentComparator;
	private int LayoutID;
	private int PageNumber;
	private String Name;
	public ItemAdapter(Context c, String name, int layoutID, int pageNumber) {
		super (c, layoutID);
		LayoutID = layoutID;
		MyApplication.getLists().put(name, new ArrayList<ListItem>());
		this.items = MyApplication.getLists().get(name);
		context = c;
		PageNumber = pageNumber;
		Name = name;
		Comparators = new ArrayList<MyComparator>();
		CurrentComparator = -1;
	}
	
	protected void AddComparator(MyComparator comp)
	{
		Comparators.add(comp);
		if(CurrentComparator == -1)
		{
			CurrentComparator = 0;
		}
	}

	
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getCount() {
		return items.size();
	}
	@Override
	public ListItem getItem(int arg0) {
		return items.get(arg0);
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View List;
		if(convertView==null){
			List=new View(context);
			LayoutInflater mLayoutinflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			List=mLayoutinflater.inflate(LayoutID, parent, false);
		}
		else{
			List = (View)convertView;
		}
			
		return List;
	}
	
	public int GetPageNmber()
	{
		return PageNumber;
	}
	
	public String GetName()
	{
		return Name;
	}
	
	public String GetTitle()
	{
		return "Missing Tilte";
	}
	
	public void AddButtonsToToolbar(View List, ListItem item)
	{
		// Resets the toolbar to be closed
		LinearLayout toolbar = (LinearLayout) List.findViewById(R.id.toolbar);
		toolbar.removeAllViews();
		if(item instanceof IShowPhoneButton && ((IShowPhoneButton)item).GetPhoneNumber()!= null && !((IShowPhoneButton)item).GetPhoneNumber().equals(""))
		{
			toolbar.addView(new CallButton(context, ((IShowPhoneButton)item).GetPhoneNumber()));
		}
		
		if(item instanceof IShowWebSiteButton && ((IShowWebSiteButton)item).GetWebSiteAddress()!= null && !((IShowWebSiteButton)item).GetWebSiteAddress().equals(""))
		{
			toolbar.addView(new WebSiteButton(context, ((IShowWebSiteButton)item).GetWebSiteAddress()));
		}
		
		if(item instanceof IShowDriveToButton)
		{
			toolbar.addView(new DriveToButton(context, ((IShowDriveToButton)item).GetLat(), ((IShowDriveToButton)item).GetLong()));
		}
		
		if(item instanceof IShowMailButton && ((IShowMailButton)item).GetMailAddress()!= null && !((IShowMailButton)item).GetMailAddress().equals(""))
		{
			toolbar.addView(new MailButton(context, ((IShowMailButton)item).GetMailAddress(), ((IShowMailButton)item).GetMailSubject(), ((IShowMailButton)item).GetMailBody()));
		}
		
		if(item instanceof IShowOnMap)
		{
			toolbar.addView(new ShowOnMapButton(context, (IShowOnMap)item));
		}
		
		if(item instanceof IShowCalendar)
		{
			toolbar.addView(new ViewCalendarButton(context, ((IShowCalendar)item).GetID()));
		}
		
		
        ((LinearLayout.LayoutParams) toolbar.getLayoutParams()).bottomMargin = -50;
        toolbar.setVisibility(View.GONE);
	}
	
	public void SetLatLon(double lat, double lon)
	{
		boolean Found = false;
		for (Comparator comp : Comparators) {
			if(comp instanceof ComparatorByDistance)
			{
				((ComparatorByDistance) comp).SetLat(lat);
				((ComparatorByDistance) comp).SetLong(lon);
				Found = true;		
			}
		}
	}


	public Comparator GetCurrentComperator() {
		if(Comparators.size()!=0 && CurrentComparator != -1)
			return Comparators.get(CurrentComparator);
		return null;
	}
	
	public int GetComparatorsCount() {
		if(Comparators.size()!=0)
			return Comparators.size();
		return 0;
	}


	public ArrayList<String> GetComparatorsTitles() {
		ArrayList<String> titles = new ArrayList<String>();
		
		if(GetComparatorsCount()!= 0)
		{
			for (MyComparator c : Comparators) {
				titles.add(c.GetTiltle());
			}
		}
		
		return titles;
	}


	public void SetComparator(int itemId) {
		CurrentComparator = itemId;
		Collections.sort(items,Comparators.get(CurrentComparator));
		this.notifyDataSetChanged();
	}
}
